<template>
  <div class="AtCater">
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button
          type="primary"
          @click="addVisible = !addVisible"
          class="buttom"
          size="mini"
          >添加分类</el-button
        >
      </div>
      <!-- 添加分类的对话框 -->
      <el-dialog
        title="添加文章分类"
        @change="onAddClosed"
        :visible.sync="addVisible"
        width="35%"
      >
        <!-- 添加的表单 -->
        <el-form
          :model="addForm"
          :rules="addRules"
          ref="addRef"
          label-width="70px"
        >
          <el-form-item label="分类名称" prop="cate_name">
            <el-input
              v-model="addForm.cate_name"
              minlength="1"
              maxlength="10"
            ></el-input>
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input
              v-model="addForm.cate_alias"
              minlength="1"
              maxlength="15"
            ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button size="mini" @click="addVisible = false">取 消</el-button>
          <el-button size="mini" type="primary" @click="addCate"
            >添 加</el-button
          >
        </span>
      </el-dialog>
      <!-- 修改文章分类的对话框 -->
      <el-dialog
        title="修改文章分类"
        :visible.sync="editVisible"
        width="35%"
        @closed="onEditClosed"
      >
        <!-- 修改的表单 -->
        <el-form
          :model="editForm"
          :rules="addRules"
          ref="editFormRef"
          label-width="100px"
        >
          <el-form-item label="分类名称" prop="cate_name">
            <el-input
              v-model="editForm.cate_name"
              minlength="1"
              maxlength="10"
            ></el-input>
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input
              v-model="editForm.cate_alias"
              minlength="1"
              maxlength="15"
            ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button size="mini" @click="editVisible = false">取 消</el-button>
          <el-button size="mini" type="primary" @click="editCate"
            >确 定</el-button
          >
        </span>
      </el-dialog>
      <!-- 表格区域 -->
      <el-table
        stype="width: 100%; font-size:12px"
        size="mini"
        :data="cateList"
        border
        stripe
      >
        <el-table-column
          label="序号"
          type="index"
          width="100"
        ></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button
              type="primary"
              @click="showEditDialog(row.id)"
              size="mini"
              >修改</el-button
            >
            <el-button type="danger" size="mini" @click="remove(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import axios from '@/project/staggerer/Dsrc/main.js'
export default {
  name: 'ArtCate',
  data() {
    const newCate = (rule, value, callback) => {
      if (!/^[a-zA-Z0-9]{1,15}$/.test(value)) {
        return callback(new Error('分类别名必须是1-15位的字母数字'))
      }
      callback()
    }
    return {
      // 文章的分类列表
      cateList: [],
      // 控制添加对话框的显示与隐藏
      addVisible: false,
      // 控制修改对话框的显示与隐藏
      editVisible: false,
      // 修改表单的数据对象
      editForm: {},
      // 添加表单的数据对象
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 添加表单的验证规则对象
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            patern: /^\S{1,10}$/,
            message: '分类别名必须是1-10位的非空字符串',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            patern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur'
          },
          { validator: newCate, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 定义获取文章分类的数据函数
    async initCateList() {
      const { data: res } = await axios.get('/my/cate/list')
      if (res.code === 0) {
        this.cateList = res.data
      }
    },
    onAddClosed() {
      this.$refs.addRef.resetFields()
    },
    // 添加文章分类
    addCate() {
      // 表单预校验
      this.$refs.addRef.validate(async valid => {
        if (!valid) return
        // 发起请求
        const { data: res } = await axios.post('/my/cate/add', this.addForm)
        if (res.code !== 0) return this.$message.error('添加分类失败!')
        this.$message.success('添加分类成功!')
        // 关闭对话框
        this.addVisible = false
        // 重新请求列表数据
        this.initCateList()
      })
    },
    // 展示变迁的对话框
    async showEditDialog(id) {
      if (id < 3) return this.$message.error('管理员不允许修改这个分类')
      // 获取分类的基本信息
      const { data: res } = await axios.get('/my/cate/info', {
        params: { id: id }
      })
      if (res.code === 0) {
        // 为 data 好的 editForm 赋值
        this.editForm = res.data
        // 展示对话框
        this.editVisible = true
      }
    },
    // 修改分类
    editCate() {
      // 表单验证
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        // 发起请求
        const { data: res } = await axios.put('/my/cate/info', this.editForm)
        // 修改失败
        if (res.code !== 0) return this.$message.error('修改分类失败')
        // 修改成功 提示用户 关闭对话框 刷新列表数据
        this.$message.success('修改分类成功')
        this.editVisible = false
        // 重新请求列表数据
        this.initCateList()
      })
    },
    onEditClosed() {
      this.$refs.editFormRef.resetFields()
    },
    // 删除分类
    async remove(id) {
      //  不允许删除前两个
      if (id < 3) return this.$message.error('管理员不允许修改这个分类')
      // 询问用户是否删除
      const confirmResult = await this.$confirm(
        '此操作将永久删除该分类,是否继续',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)

      if (confirmResult === 'cancel') return
      // 发起请求删除对应的数据
      const { data: res } = await axios.delete('/my/cate/del', {
        params: { id: id }
      })
      // 判断是否删除成功
      if (res.code !== 0) return this.$message.error('删除分类失败')
      this.$message.success('删除分类成功')
      // 重新请求列表数据
      this.initCateList()
    }
  },
  created() {
    // 在created 中获取数据
    this.initCateList()
  }
}
</script>

<style lang="less">
.AtCater {
  .el-form-item__label {
    font-size: 12px;
  }
  .header-box {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .header-box::after {
    content: none !important;
  }

  // 全局重置 el-table 的样式
  .el-table thead {
    color: #000;
  }

  .el-dialog__body {
    padding: 10px 20px;
  }
  // 重置卡片的基础样式
  .el-card.is-always-shadow,
  .el-card.is-hover-shadow:focus,
  .el-card.is-hover-shadow:hover {
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    font-size: 13px;
  }
}
</style>
